home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / gnu_oleo_1_2_2.lha / oleo-1.2.2 / panic.c < prev    next >
C/C++ Source or Header  |  1993-03-03  |  3KB  |  160 lines

  1. /*    Copyright (C) 1990, 1992, 1993 Free Software Foundation, Inc.
  2.  
  3. This file is part of Oleo, the GNU Spreadsheet.
  4.  
  5. Oleo is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. Oleo is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with Oleo; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include <stdio.h>
  20. #include "funcdef.h"
  21. #include "sysdef.h"
  22.  
  23. #include "global.h"
  24. #include "io-generic.h"
  25. #include "io-abstract.h"
  26.  
  27.  
  28. extern char **environ;
  29. #ifdef __STDC__
  30. extern int dup (int);
  31. extern int close (int);
  32. #ifndef AMIGA
  33. extern VOIDSTAR sbrk (size_t);
  34. extern VOIDSTAR brk (VOIDSTAR);
  35. #endif
  36. #else
  37. extern int dup ();
  38. extern int close ();
  39. extern VOIDSTAR sbrk ();
  40. extern VOIDSTAR brk ();
  41. #endif
  42.  
  43.  
  44.  
  45. void
  46. panic_write_file (fp, rng)
  47.      FILE *fp;
  48.      struct rng *rng;
  49. {
  50.   int fd;
  51.   VOIDSTAR datend;
  52.   VOIDSTAR datstart;
  53.   unsigned long cnt;
  54.  
  55.   if (rng)
  56.     {
  57.       io_error_msg ("Can't write partial panic-save files");
  58.       return;
  59.     }
  60.   fd = dup (fileno (fp));
  61.   if (fd < 0)
  62.     {
  63.       io_error_msg ("Couldn't dup save file");
  64.       return;
  65.     }
  66.   datstart = (VOIDSTAR) (&environ + sizeof (char **));
  67. #ifndef AMIGA
  68.   datend = sbrk (0);
  69. #else
  70.   datend = (char *) -1;
  71. #endif
  72.   if (datend == (char *) -1)
  73.     {
  74.       io_error_msg ("Couldn't sbrk(0)!");
  75.       close (fd);
  76.       return;
  77.     }
  78.  
  79.   cnt = (char *) datend - (char *) datstart;
  80.   if (write (fd, &cnt, sizeof (cnt)) != sizeof (cnt))
  81.     {
  82.       io_error_msg ("Couldn't write %lu (%d bytes) to save file", cnt, sizeof (cnt));
  83.       close (fd);
  84.       return;
  85.     }
  86.   if (write (fd, datstart, cnt) != cnt)
  87.     {
  88.       io_error_msg ("Couldn't write %lu bytes to save file", cnt);
  89.       close (fd);
  90.       return;
  91.     }
  92.   if (close (fd) < 0)
  93.     {
  94.       io_error_msg ("Couldn't close save file");
  95.       return;
  96.     }
  97. }
  98.  
  99. void
  100. panic_read_file (fp, ismerge)
  101.      FILE *fp;
  102.      int ismerge;
  103. {
  104.   int fd;
  105.   unsigned long cnt;
  106.   VOIDSTAR datstart;
  107.  
  108.   if (ismerge)
  109.     {
  110.       io_error_msg ("Can't merge panic-save files");
  111.       return;
  112.     }
  113.   if ((fd = dup (fileno (fp))) < 0)
  114.     {
  115.       io_error_msg ("Couldn't dup save file");
  116.       return;
  117.     }
  118.   if (read (fd, (VOIDSTAR) & cnt, sizeof (cnt)) != sizeof (cnt))
  119.     {
  120.       io_error_msg ("Couldn't read data_size (%d bytes) from save file", sizeof (cnt));
  121.       close (fd);
  122.       return;
  123.     }
  124.   datstart = (VOIDSTAR) (&environ + sizeof (char **));
  125. #ifndef AMIGA
  126.   if (brk ((char *) datstart + cnt) == (VOIDSTAR) - 1)
  127. #else
  128.   if (-1)
  129. #endif
  130.     {
  131.       io_error_msg ("Couldn't allocate %lu bytes of memory", cnt);
  132.       close (fd);
  133.       return;
  134.     }
  135.   if (read (fd, datstart, cnt) != cnt)
  136.     {
  137.       io_error_msg ("Couldn't read in %lu bytes of data", cnt);
  138.       close (fd);
  139.       return;
  140.     }
  141.   if (close (fd) < 0)
  142.     io_error_msg ("Couldn't close save file");
  143.   io_recenter_all_win ();
  144. }
  145.  
  146. int
  147. panic_set_options (set_opt, option)
  148.      int set_opt;
  149.      char *option;
  150. {
  151.   return -1;
  152. }
  153.  
  154.  
  155. void
  156. panic_show_options ()
  157. {
  158.   io_text_line ("File format:  panic save   (quick-n-dirty data-segment dump)");
  159. }
  160.